package mysql

import (
	"fmt"
	"goconfcenter/models"
	"goconfcenter/service/dto/bus"
	"time"

	"go.uber.org/zap"
)

type BackendUserDao struct {
}

func BackendUserDaoInstance() *BackendUserDao {
	return &BackendUserDao{}
}
func (c *BackendUserDao) Get(userId int64) *models.BackendUser {
	sql := "select *  from rms_backend_user where  id=? "
	user := models.BackendUser{}
	result := db.Raw(sql, userId).First(&user)
	if result.Error == nil {
		return &user
	} else {
		zap.L().Error("查询出错", zap.Error(result.Error))
		return nil
	}
}
func (c *BackendUserDao) GetByUserName(userName string) *models.BackendUser {
	sql := "select *  from rms_backend_user where  user_name=? "
	user := models.BackendUser{}
	result := db.Raw(sql, userName).First(&user)
	if result.Error == nil {
		return &user
	} else {
		zap.L().Error("查询出错", zap.Error(result.Error))
		return nil
	}
}

func (c *BackendUserDao) PageByQuery(params *bus.BackendUserQuery) ([]*models.BackendUserPoJo, int64) {
	var total int64
	whereSql := " 1= 1 and rms_backend_user.del_flag=0"
	data := make([]*models.BackendUserPoJo, 0)
	if params.UserName != "" {
		whereSql = whereSql + fmt.Sprintf(" and rms_backend_user.user_name like %q ", ("%"+params.UserName+"%"))
	}
	if params.RealName != "" {
		whereSql = whereSql + fmt.Sprintf(" and rms_backend_user.real_name like %q ", ("%"+params.RealName+"%"))
	}
	//默认排序
	sortOrder := "ID"
	switch params.SortOrder {
	case "ID":
		sortOrder = "id"
	}
	if params.SortOrder == "desc" {
		sortOrder = "  " + sortOrder
	}
	// 分页查询
	offset := (params.PageNum - 1) * params.PageSize
	query := db.Table("rms_backend_user").Select(" rms_backend_user.*,T1.real_name As creatorName,T2.real_name As updaterName ")
	query = query.Joins(" Inner Join rms_backend_user As T1 on T1.id = rms_backend_user.creator ")
	query = query.Joins(" Inner Join rms_backend_user As T2 on T2.id = rms_backend_user.updater ")
	err := query.Where(whereSql).Count(&total).Limit(params.PageSize).Offset(offset).Order(sortOrder).Find(&data).Error
	if err != nil {
		fmt.Println(err)
	}
	return data, total
}

func (c *BackendUserDao) GetListByQuery(params *bus.BackendUserQuery) []*models.BackendUserPoJo {
	whereSql := " 1= 1 and rms_backend_user.del_flag=0 "
	data := make([]*models.BackendUserPoJo, 0)
	if params.UserName != "" {
		whereSql = whereSql + fmt.Sprintf(" and rms_backend_user.user_name = %q ", params.UserName)
	}
	if params.RealName != "" {
		whereSql = whereSql + fmt.Sprintf(" and rms_backend_user.real_name = %q ", params.RealName)
	}
	query := db.Table("rms_backend_user").Select(" rms_backend_user.*,T1.real_name As creatorName,T2.real_name As updaterName ")
	query = query.Joins(" Inner Join rms_backend_user As T1 on T1.id = rms_backend_user.creator ")
	query = query.Joins(" Inner Join rms_backend_user As T2 on T2.id = rms_backend_user.updater ")
	err := query.Where(whereSql).Find(&data).Error
	if err != nil {
		zap.L().Error("数据库查询出错", zap.Error(err))
	}
	return data
}

func (c *BackendUserDao) Insert(user *models.BackendUser) *models.BackendUser {
	result := db.Create(&user)
	if result.Error == nil {
		return user
	} else {
		zap.L().Error("数据插入失败", zap.Error(result.Error))
		return nil
	}
}

func (c *BackendUserDao) Update(user *models.BackendUser) bool {
	result := db.Save(&user)
	if result.Error == nil {
		return true
	} else {
		zap.L().Error("数据更新失败", zap.Error(result.Error))
		return false
	}
}
func (c *BackendUserDao) BathDelete(ids *[]int64, operatorId int64) bool {
	updateModels := models.BackendUser{
		Updater:    operatorId,
		UpdateTime: time.Now(),
		DelFlag:    1,
	}
	result := db.Model(&models.BackendUser{}).Where(*ids).Updates(&updateModels)
	if result.Error == nil {
		return true
	} else {
		zap.L().Error("数据批量更新失败", zap.Error(result.Error))
		return false
	}
}
